<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Shop\Api;

use \Common\Api\Api;
use Shop\Model\OrderCommentModel;

class OrderCommentApi extends Api{

    /**
     * 查询，不分页
     */
    const QUERY_NO_PAGING  = "Shop/OrderComment/queryNoPaging";
    /**
     * 商品评价查询
     */
    const QUERY_BY_PRODUCT = "Shop/OrderComment/queryByProduct";
    /**
     * 添加
     */
    const ADD = "Shop/OrderComment/add";
    /**
     * 保存
     */
    const SAVE = "Shop/OrderComment/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Shop/OrderComment/saveByID";

    /**
     * 删除
     */
    const DELETE = "Shop/OrderComment/delete";

    /**
     * 查询
     */
    const QUERY = "Shop/OrderComment/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "Shop/OrderComment/getInfo";
    /**
     * 添加多个评分
     */
    const Add_ARRAY = "Shop/OrderComment/addArray";


	protected function _init(){
		$this->model = new OrderCommentModel();
	}

	//商品评价查询 
	//c:itboye_order_comment i:itboye_orders_item m:common_member 
	public function queryByProduct($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){
		$query = $this->model->alias('c')->join('LEFT JOIN itboye_orders_item as i on i.p_id=c.product_id and i.order_code=c.order_code')->join('LEFT JOIN common_member as m c.user_id=m.uid');
		if(!is_null($map)) $query = $query->where($map);
		if(false !== $order) $query = $query->order($order);
		if(false !== $fields) $query = $query->field($fields);
		$list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();
		if ($list === false) 
			return $this -> apiReturnErr($this -> model -> getDbError());

		$count = $this -> model -> where($map) -> count();
		// 查询满足要求的总记录数
		$Page = new \Think\Page($count, $page['size']);

		//分页跳转的时候保证查询条件
		if ($params !== false) {
			foreach ($params as $key => $val) {
				$Page -> parameter[$key] = urlencode($val);
			}
		}

		// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page -> show();
		return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
	}
    /**
     * 添加多个评分
     * @param $orders_id 订单ID
     * @param $uid 用户ID
     * @param $pid_arr 产品数组－评分
     * @param $score_arr 评分数组
     * @param $text_arr 评价内容
     * @return array
     */
	public function addArray($orders_id,$uid,$pid_arr,$score_arr,$logistics_service_arr,$delivery_speed_arr,$service_attitude_arr,$text_arr){

		$this->model->startTrans();

		$flag = true;
		$error = "";
		$insert_ids = array();
		$nowtime = time();
		foreach($pid_arr as $key=>$id){
			$entity = array(
				'product_id'=>$id,
				'order_code'=>$orders_id,
				'score'=>$score_arr[$key],
				'logistics_service'=>$logistics_service_arr[$key],
				'delivery_speed'=>$delivery_speed_arr[$key],
				'service_attitude'=>$service_attitude_arr[$key],
				'comment'=>$text_arr[$key],
				'user_id'=>$uid,
				'createtime'=>$nowtime
			);




			if($this->model->create($entity,1)){

				$result = $this->model->add($entity);

				if($result === false){
					$flag = false;
					$error = $this->model->getDbError();
				}else{
					apiCall(OrdersApi::SAVE_BY_ID,array($orders_id,array('comment_status'=>1)));
					array_push($insert_ids,$result);
				}

			}else{
				$flag = false;
				$error = $this->model->getError();
			}


		}


		if($flag){
			$this->model->commit();
			return $this->apiReturnSuc($insert_ids);
		}else{
			$this->model->rollback();
			return $this->apiReturnErr($error);
		}

	}
}

